package game;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import util.Pair;


/**
 * MatchStatistics class
 * 
 * Contains match related statistics.
 */
public class MatchStatistics implements Serializable {

	public MatchStatistics() {
		rankingPoints = new ArrayList<Pair<String, Integer>>();
		score = new ArrayList<Pair<String, Integer>>();
		players = new ArrayList<String>();
	}
	

	public MatchStatistics(Match match) {
		this();
		
		this.moveCount = match.countMoves();
		this.dateStarted = match.getDateStarted();
		this.matchId = match.getId();
		this.gameName = match.getClass().getSimpleName();
		
		for (Player player : match.getPlayers()) {
			this.players.add(player.getLogin());
		}
		
		if (match.isFinished()) {
			this.dateEnded = match.getDateEnded();
			this.winner = match.getWinner() != null ? match.getWinner().getLogin() : null;
		
			for (Pair<Player, Integer> pair : match.getRankingPoints()) {
				this.rankingPoints.add(new Pair<String, Integer>(pair.getFirst().getLogin(), pair.getSecond()));
			}
			
			for (Pair<Player, Integer> pair : match.getScore()) {
				this.score.add(new Pair<String, Integer>(pair.getFirst().getLogin(), pair.getSecond()));
			}
		}
	}
	
	
	/**
	 * Returns the list of participants for this match.
	 * 
	 * @return The list of players' logins.
	 */
	public List<String> getPlayers() {
		if (players != null && !players.isEmpty())
			return players;
		
		List<String> list = new ArrayList<String>();
		
		for (Pair<String, Integer> pair : this.score) {
			list.add(pair.getFirst());
		}
		
		return list;
	}
	
	
	/**
	 * Is the match over?
	 * 
	 * @return True if the match is over, false otherwise.
	 */
	public boolean isFinished() {
		return winner != null;
	}
	
	
	/**
	 * Who was the winner?
	 * 
	 * @return The login of the player who has won.
	 */
	public String getWinner() {
		return this.winner;
	}
	
	
	public void setWinner(String winner) {
		this.winner = winner;
	}
	
	
	/**
	 * Returns a list of Pair's containing the ranking points obtained by each
	 * player.
	 * 
	 * @return A List of Pair's containing the ranking points obtained by each player.
	 */
	public List<Pair<String, Integer>> getRankingPoints() {
		return this.rankingPoints;
	}
	
	
	public void setRankingPoints(List<Pair<String, Integer>> rankingPoints) {
		this.rankingPoints = rankingPoints;
	}
	
	
	/**
	 * Returns a list of Pair's containing the score obtained by each player.
	 * 
	 * @return A list of Pair's containing the score obtained by each player.
	 */
	public List<Pair<String, Integer>> getScore() {
		return score;
	}
	
	
	public void setScore(List<Pair<String, Integer>> score) {
		this.score = score;
	}
	
	
	/**
	 * Counts the number of moves performed during the game.
	 * 
	 * @return The number of moves performed during the game.
	 */
	public int countMoves() {
		return this.moveCount;
	}
	
	
	public void setMoveCount(int moveCount) {
		this.moveCount = moveCount;
	}
	
	
	/**
	 * When did this game start?
	 * 
	 * @return The date the game started.
	 */
	public Date getDateStarted() {
		return this.dateStarted;
	}
	
	
	public void setDateStarted(Date dateStarted) {
		this.dateStarted = dateStarted;
	}
	
	
	/**
	 * When did this game ended?
	 * 
	 * @return The date the game ended.
	 */
	public Date getDateEnded() {
		return this.dateEnded;
	}
	
	
	public void setDateEnded(Date dateEnded) {
		this.dateEnded = dateEnded;
	}
	
	
	/**
	 * Returns the match identifier.
	 * 
	 * @return The match identifier.
	 */
	public int getMatchId(){
		return this.matchId;
	}
	
	
	public void setMatchId(int matchId) {
		this.matchId = matchId;
	}
	
	
	/**
	 * What's the name of the game that was played?
	 * 
	 * @return The name of the game that was played?
	 */
	public String getGameName() {
		return this.gameName;
	}
	
	
	public void setGameName(String gameName) {
		this.gameName = gameName;
	}
	
	
	public String toString() {
		return "\nMatch : " + matchId + "\n"
			+ "players        = " + getPlayers().toString() + "\n"
			+ "winner         = " + winner + "\n"
			+ "gameName       = " + gameName + "\n"
			+ "dateStarted    = " + dateStarted + "\n"
			+ "dateEnded      = " + dateEnded + "\n"
			+ "moveCount      = " + moveCount + "\n"
			+ "ranking points = " + rankingPoints.toString() + "\n"
			+ "score          = " + score.toString() + "\n\n";
	}
	
	
	private int moveCount;
	private String winner;
	private List<String> players;
	private List<Pair<String, Integer>> rankingPoints;
	private List<Pair<String, Integer>> score;
	private Date dateStarted, dateEnded;
	private int matchId;
	private String gameName;
	
	private static final long serialVersionUID = 1L;
}
